package com.xizi.chatBot.service.impl;

import com.xizi.chatBot.model.ChatInfo;
import com.xizi.chatBot.service.IChatHistoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class ChatHistoryServiceImpl implements IChatHistoryService {
    private static final Logger log = LoggerFactory.getLogger(ChatHistoryServiceImpl.class);

    HashMap<String,String> history = new LinkedHashMap<>();

    @Override
    public void save(String chatId, String prompt) {
        log.info("conversationId: {}, prompt: {}", chatId, prompt);

        history.put(chatId, prompt);
    }

    @Override
    public List<ChatInfo> get() {
        List<ChatInfo> chatInfos = new ArrayList<>();
        for (String chatId : history.keySet()) {
            ChatInfo chatInfo = new ChatInfo(chatId, history.get(chatId));
            chatInfos.add(chatInfo);
        }
        return chatInfos;
    }

    @Override
    public void clear(String chatId) {
        history.remove(chatId);
    }
}
